moddbfandomcom-20200213-history
QBasic:Mouse
Overview QBasic is a tremendous improvement over previous versions of Basic interpreters, yet it still lacks any support for mouse functions outside the QBasic editor screen itself. Fortunately, while QBasic does not include any commands for accessing the mouse in user programs, it DOES include commands for incorporating machine language code into our programs, which in turn allows us to access mouse functions by way of the sub-program listed below: SUB Mouse (Funk) SHARED B, H, V POKE 100, 184: POKE 101, Funk: POKE 102, 0 POKE 103, 205: POKE 104, 51: POKE 105, 137 POKE 106, 30: POKE 107, 170: POKE 108, 10 POKE 109, 137: POKE 110, 14: POKE 111, 187 POKE 112, 11: POKE 113, 137: POKE 114, 22 POKE 115, 204: POKE 116, 12: POKE 117, 203 CALL Absolute(100) B = PEEK(&HAAA) H = PEEK(&HBBB) + PEEK(&HBBC) * 256 V = PEEK(&HCCC) + PEEK(&HCCD) * 256 END SUB Requirements For this sub-program to work properly, the following items are required; * A Microsoft or compatible mouse * A Microsoft or compatible mouse driver Using the Mouse Sub-program To use this sub-program in your own QBasic programs, simply copy and paste the code into your program, along with the following definition and declaration: DEFINT A-Z 'Make variables integer by default DECLARE SUB Mouse (Funk) 'Declare Mouse functions Then, call the sub-program with a parameter of 1, 2 or 3, which corresponds to the desired mouse function as follows; Parameter: / Function: 1. Show mouse cursor 2. Hide mouse cursor 3. Read mouse buttons and coordinates Example Program The following program displays the mouse cursor, paints the screen blue,then waits for the user to press one or both of the mouse buttons. When the left button is pressed, the program draws a yellow dot under the mouse cursor. Pressing the right button clears the screen, while pressing both ends the program. ''''' Initialize Program ''''''' DEFINT A-Z 'Make variables integer by default DECLARE SUB Mouse (Funk) 'Declare Mouse functions SCREEN 13 'Set screen to graphics mode PALETTE 0, 3145728 'Set background to blue Mouse 1 'Show mouse cursor ''''''' Main Program '''''' DO 'Start of main program loop Mouse 3 'Read mouse buttons & position IF B > 0 THEN 'If a button is pressed then, Mouse 2 'Hide mouse cursor Mouse 3 'Read mouse buttons & position SELECT CASE B 'Which button is pressed? CASE 1: PSET (H \ 2, V), 14 'Left button - draw a dot CASE 2: CLS 'Right button - clear screen CASE 3: SYSTEM 'Both buttons - end program END SELECT '( End of Select structure ) Mouse 1 'Show mouse cursor END IF '( End of If structure ) LOOP 'Back to start of program '''''' Sub-Program ''''''' SUB Mouse (Funk) SHARED B, H, V POKE 100, 184: POKE 101, Funk: POKE 102, 0 POKE 103, 205: POKE 104, 51: POKE 105, 137 POKE 106, 30: POKE 107, 170: POKE 108, 10 POKE 109, 137: POKE 110, 14: POKE 111, 187 POKE 112, 11: POKE 113, 137: POKE 114, 22 POKE 115, 204: POKE 116, 12: POKE 117, 203 CALL Absolute(100) B = PEEK(&HAAA) H = PEEK(&HBBB) + PEEK(&HBBC) * 256 V = PEEK(&HCCC) + PEEK(&HCCD) * 256 END SUB Important Notes! * This program defines the following global variables: # B ( Button status ) # H ( Horizontal mouse coordinate ) # V ( Vertical mouse coordinate ) If you wish to use these names for other variables elsewhere in your program, be sure to re-name the globals variables in the mouse sub-program. * Due to a quirk in the Microsoft mouse driver, the effect of function 2 is cumulative. In other words, if you call mouse function 2 to hide the mouse cursor six times, you must also call function 1 six times to display it again. HOWEVER, function 1 is NOT cumulative. This means that a single call to function 2 will ALWAYS hide the cursor. Therefore, you must take care not to hide the cursor more than once before you attempt to display it again. * Be sure to hide the mouse cursor before performing any graphics functions on the screen, or else the artwork underneath the cursor will be garbled.